package feri.lenobe;

import java.security.AccessControlException;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Nastavitve extends Activity {
	
	//GUI
	private EditText txtHitrostUcenja;
	private EditText txtStVhodnih;
	private EditText txtStSkritih;
	private EditText txtDovoljenaNapaka;
	private Button btnShrani;
	private Button btnPreklici;
	private Button btnPocisti;
	public boolean spremembaVrednosti;
	private boolean JeNaucenaMreza;
	
	public static final String PREFS_NAME = "NastavitvePrevajalca";
	public boolean UseDeafault;
	
	//Privzete vrednosti
	public static final float defaultHitrostUcenja = 0.20f;
	public static final int defaultStVhodnihN = 76;
	public static final int defaultStSkritihN =5;
	public static final float defaultDovoljenaNapaka = 0.05f;
	
	//Upravlanje z nastavitvami
	public SharedPreferences.Editor editor;
	SharedPreferences settings;
    
	public void onCreate(Bundle savedInstanceState) throws AccessControlException {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nastavitve);
        
        spremembaVrednosti = false;
        
        //Bindamo spremenljivke na GUI
        txtHitrostUcenja = (EditText) findViewById(R.id.editTextHitrostUcenja);
        txtStVhodnih = (EditText) findViewById(R.id.editStVhodnih);
        txtStSkritih = (EditText) findViewById(R.id.editStSkritih);
        txtDovoljenaNapaka = (EditText) findViewById(R.id.editDovoljenaNapaka);
        btnShrani = (Button) findViewById(R.id.buttonShraniN);
        btnPreklici = (Button) findViewById(R.id.buttonPrekliciN);
        btnPocisti = (Button) findViewById(R.id.pocisti);
        
        //Nalozi nastavitve
        settings = getSharedPreferences(PREFS_NAME, 0);
        editor = settings.edit();
        UseDeafault = settings.getBoolean("privzeteVrednosti", true);
        
        if(UseDeafault)
        {
        	//Nastavitev ni, zato uporabimo privzete vrednosti:
        	// -Hitrost učenja:			0.15f
        	// -Št. vhodnih nevronov:	20
        	// -Št. skritih nevronov:	15
        	// -Dovoljena napaka:		0.01f
        	editor.putBoolean("privzeteVrednosti", true);
        	editor.putFloat("hitrostUcenja", defaultHitrostUcenja);
        	editor.putInt("stVhodnihN", defaultStVhodnihN);
        	editor.putInt("StSkritihN", defaultStSkritihN);
        	editor.putFloat("dovoljenaNapaka", defaultDovoljenaNapaka);
        	editor.putBoolean("JeNaucenaMreza", false);
        	editor.commit();
        
        	txtHitrostUcenja.setText(Float.toString(defaultHitrostUcenja));
        	txtStSkritih.setText(Integer.toString(defaultStSkritihN));
        	txtStVhodnih.setText(Integer.toString(defaultStVhodnihN));
        	txtDovoljenaNapaka.setText(Float.toString(defaultDovoljenaNapaka));
        	btnPocisti.setEnabled(false);
        }
        else
        {
        	//Nastavitve so prisotne zato jih naložimo v formo
        	txtHitrostUcenja.setText(Float.toString(settings.getFloat("hitrostUcenja", defaultHitrostUcenja)));
        	txtStVhodnih.setText(Integer.toString(settings.getInt("stVhodnihN",defaultStVhodnihN)));
        	txtStSkritih.setText(Integer.toString(settings.getInt("StSkritihN",defaultStSkritihN)));
        	txtDovoljenaNapaka.setText(Float.toString(settings.getFloat("dovoljenaNapaka", defaultDovoljenaNapaka)));
        	btnPocisti.setEnabled(settings.getBoolean("JeNaucenaMreza", false));
        }
        
        //GUMB PREKLIC (nadomesti gumb nazaj)
        btnPreklici.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	finish();
            }
        });
        
        //GUMB SHRANI (shrani morebitne spremebe)
        btnShrani.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				if(spremembaVrednosti)
				{
		        	editor.putBoolean("privzeteVrednosti", false);
		        	editor.putFloat("hitrostUcenja", Float.parseFloat(txtHitrostUcenja.getText().toString()));
		        	editor.putInt("stVhodnihN", Integer.parseInt(txtStVhodnih.getText().toString()));
		        	editor.putInt("StSkritihN", Integer.parseInt(txtStSkritih.getText().toString()));
		        	editor.putFloat("dovoljenaNapaka", Float.parseFloat(txtDovoljenaNapaka.getText().toString()));
		        	editor.commit();
		        	finish();
				}
			}
		});
        
        btnPocisti.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				if(spremembaVrednosti)
				{
		        	editor.putBoolean("privzeteVrednosti", true);
		        	editor.putBoolean("JeNaucenaMreza", false);
		        	btnPocisti.setEnabled(false);
		        	editor.commit();
		        	finish();
				}
			}
		});
        
        //Edit text Hitrost učenja - zaznava sprememb
        txtHitrostUcenja.addTextChangedListener(new TextWatcher() { 
        	 public void  afterTextChanged (Editable s){spremembaVrednosti=true;}
        	 public void  beforeTextChanged  (CharSequence s, int start, int count, int after){}
             public void  onTextChanged  (CharSequence s, int start, int before, int count){spremembaVrednosti=true;}
        });
        //Edit text št. vhodnih nevronov - zaznava sprememb
        txtStVhodnih.addTextChangedListener(new TextWatcher() { 
             public void  afterTextChanged (Editable s){spremembaVrednosti=true;}
             public void  beforeTextChanged  (CharSequence s, int start, int count, int after){}

             public void  onTextChanged  (CharSequence s, int start, int before, int count){spremembaVrednosti=true;}
        });
        //Edit text št. izhodnih nevronov - zaznava sprememb
        txtStSkritih.addTextChangedListener(new TextWatcher() { 
             public void  afterTextChanged (Editable s){spremembaVrednosti=true;}
             public void  beforeTextChanged  (CharSequence s, int start, int count, int after){}
             public void  onTextChanged  (CharSequence s, int start, int before, int count){spremembaVrednosti=true;}
        });
        //Edit text dovoljena napaka - zaznava sprememb
        txtDovoljenaNapaka.addTextChangedListener(new TextWatcher() { 
             public void  afterTextChanged (Editable s){spremembaVrednosti=true;}
             public void  beforeTextChanged  (CharSequence s, int start, int count, int after){}
             public void  onTextChanged  (CharSequence s, int start, int before, int count){spremembaVrednosti=true;}
        });
        
    }
}